<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      :close-on-click-modal="false"
      :title="mytitle"
      :modal-append-to-body="false"
      :visible.sync="visible"
      :show="show"
      :before-close="closeDialog"
      @close="$emit('update:show',false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" label-width="120px">
        <el-form-item label="期数名称" prop="stage_title">
          <el-col :span="12">
            <el-input placeholder v-model="data.stage_title" clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="签到日期">
          <el-col :span="12">
            <el-date-picker
              v-model="data.end_time"
              type="date"
              value-format="timestamp"
              placeholder="选择日期"
              style="width: 100%;"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        
        <el-form-item>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click="subForm('data')"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped></style>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "",
          label: ""
        }
      ],
      mytitle: "",
      fileList2: [],
      updateUrl: "",
      showImg: true,
      value: "",
      data: {
        stage_title: "",
        end_time: ""
      },
      loading: false,
      dataYZ: {
        stage: [{ required: true, message: "请输入标题", trigger: "blur" }]
      },
      visible: this.show
    };
  },
  props: {
    show: Boolean,
    editId: String,
    clickNum: Number
  },
  methods: {
    getqihao() {
      this.loading = true;
      var params = {};
      this.$api.getPlugin.getQIhao(params).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          this.data.stage = res.data.result;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
        this.loading = false;
      });
    },
    uploadSuccess(response, file, fileList) {
      if (response.code == 200 && response.code_desc == "请求成功") {
        this.$message.success("上传成功！");
        if (response.result.indexOf("http") <= 0) {
          this.showImg = true;
          this.data.pic = response.result;
          this.updateUrl = "/api/" + response.result;
        }
      } else if (response.code == 400) {
        this.$message.error("上传失败！不支持的类型");
      }
    },
    uploadError(response) {
      if ((response.code = 400)) {
        this.$message.error("无效请求");
      }
    },
    getOption() {
      var data = {};
      this.$api.getAdminuser.getPluginCateOption(data).then(res => {
        this.options = res.data.result;
        console.log(this.options);
      });
    },
    handChange(val) {
      console.log(val);
      this.data.cate = val;
    },

    closeDialog() {
      this.visible = false;
    },
    getDetails(id) {
      var data = {};
      data["id"] = id;
      this.$api.getNews.PacketDetail(data).then(res => {
        this.data = res.data.result;
        this.data.id = id;
        console.log(this.data);
      });
    },
    subForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.editId == undefined) {
            this.AddNews();
          } else {
            this.UpdateNews();
          }
        }
      });
    },
    AddNews() {
      // this.data.begin_time = this.$fnc.vuetime(this.data.begin_time);
      // this.data.end_time = this.$fnc.vuetime(this.data.end_time);
      var params = {};
      params["stage_title"] = this.data.stage_title;
      params["end_time"] = this.$fnc.phptime(this.data.end_time);
      this.$api.getPlugin.earlySaveAdd(params).then(res => {
        if (res.data.status == 1 && res.data.code == 200) {
          this.$fnc.alertSuccess({ text: "添加成功" });
          this.$emit("parent", "这里刷新父页面");
          this.visible = false;
        } else {
          if (res.data.result == "stage已存在") {
            res.data.result = "期数已存在";
          }
          this.$fnc.alertError(res.data.result, res.data.code_desc);
        }
      });
    },
    UpdateNews(formName) {
      console.log("更新中。。");
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.data.id = this.editId;
          this.$api.getPlugin.earlySaveEdit(this.data).then(res => {
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "编辑成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    show() {
      this.getqihao();
      console.log(this.show);
      this.visible = this.show;
    },
    clickNum(id) {
      console.log("点击button次数", id);
      console.log("编辑ID=" + this.editId);
      if (this.editId !== undefined) {
        this.mytitle = "编辑猜猜乐";
        this.getDetails(this.editId);
      } else {
        this.mytitle = "添加期数";
      }
    }
  },
  mounted() {},
  created() {
    this.getqihao();
  },
  updated() {}
};
</script>
